<?php
Yii::import('ext.helpers.EDownloadHelper');
class JournalsController extends Controller
{
	public $layout='//layouts/admin/main';

	public function init(){
        Yii::app()->user->loginUrl = array('/admin/site/login');
        parent::init();
    }

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('login','logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update', 'index', 'view', 'download'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionDownload()
	{
		$journal = Journals::model()->find('id = :id', array(':id'=>$_GET['id']));
		$path = bu('assets/journals/').$journal->content;

		EDownloadHelper::download(Yii::getPathOfAlias('webroot.assets.journals').DIRECTORY_SEPARATOR.$journal->content);
	}

	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	public function actionCreate()
	{
		$model=new Journals;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Journals']))
		{
			$model->attributes=$_POST['Journals'];
			$model->content=CUploadedFile::getInstance($model,'upload');

			if($model->save())
			{
				$model->content->saveAs(Yii::app()->basePath. '/../assets/journals/' . $model->content);

        Yii::app()->user->setFlash('success', 'Success');
        Yii::app()->user->setFlash('content', 'You have successfully upload new journal.');

				$this->redirect(array('index'));
			}
      Yii::app()->user->setFlash('error', 'Error');
      Yii::app()->user->setFlash('content', 'You can only upload PDF format files only.');
		}

		$this->redirect(array('index'));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
        $journal = Journals::model()->find($_GET['id']);
        $path=Yii::app()->basePath. '/../assets/journals/' . $journal->content;
        if ($journal->delete())
        {
          if($path){ unlink($path); }
          Yii::app()->user->setFlash('success', 'Success');
          Yii::app()->user->setFlash('content', 'You have successfully deleted a journal.');

          $this->redirect(bu('admin/journals/'));
        }

        Yii::app()->user->setFlash('error', 'Error');
        Yii::app()->user->setFlash('content', 'Delete unsuccessfull. Please try again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$model = new Journals;
    $journals = Journals::model()->with('member')->findAll();
    $members = Members::model()->findAll();
		$this->render('index',array(
			'journals'=>$journals,
			'model'=>$model,
      'members' => $members,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Catalogs::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='catalogs-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
